package com.zjc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjc.common.R;
import com.zjc.common.TimeUtils;
import com.zjc.pojo.*;
import com.zjc.service.AbsentTableService;
import com.zjc.service.StuRoomService;
import com.zjc.service.StudentService;
import com.zjc.service.VisitTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-09-17
 */
@RestController
@RequestMapping("/api/absent")
public class AbsentTableController {
	@Autowired
	AbsentTableService absentTableService;
	@Autowired
	VisitTableService visitTableService;
	@Autowired
	StudentService studentService;
	@Autowired
	StuRoomService stuRoomService;
	//获取所有申请记录，给宿管查看
	@GetMapping("/find")
	public R<Page> GetVrRecordPage(int pageNum, int pageSize, String search,String absStatus){
		QueryWrapper<AbsentTable> wrapper = new QueryWrapper<>();
		if(search!=null){
			wrapper.like("stu_name",search);
		}
		if(absStatus!=null){
			wrapper.eq("abs_status",Integer.valueOf(absStatus));
		}
		Page<AbsentTable> page1 = new Page<>(pageNum,pageSize);
		absentTableService.page(page1,wrapper);
		return R.success(page1);
	}
	//t通过学生学号找到该学生所申请过的记录
	@GetMapping("/findByStuId")
	public R<List<AbsentTable>> getAbsentbyid(int stuId){
		LambdaQueryWrapper<AbsentTable> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(AbsentTable::getStuId,stuId);
		List<AbsentTable> lists = absentTableService.list(lambdaQueryWrapper);
		return R.success(lists);
	}
	//添加申请
	@PostMapping("/applyadd")
	public R<String> AddApply(@RequestBody AbsentTable at){

		Integer roomid = at.getRoomId();
		Integer stuid = at.getStuId();
		String stuname = at.getStuName();
		if(!studentService.isStuExists(stuid)){
			return R.error("学号不正确！！");
		}
		if(!studentService.isRoomExists(roomid)){
			return R.error("房间号不存在！");
		}
		if(!visitTableService.isMatch4Stuid2name(stuid,stuname)){
			return R.error("姓名学号不匹配！");
		}
		LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(Student::getStuId,stuid);
		Student student = studentService.getOne(lambdaQueryWrapper);
		if(!student.getRoomId().equals(roomid)){
			return R.error("房间号有误！");
		}
		absentTableService.save(at);
		return R.success("添加申请成功");
	}
	@PutMapping("/update")
	public R<String> updateStatus(@RequestBody AbsentTable at){
		absentTableService.updateById(at);
		return R.success("修改成功");
	}
	@GetMapping("/total")
	public R<Long> getAbsTotal(){
		Long num = absentTableService.count();
		return R.success(num);
	}
	@GetMapping("/todayadd")
	public R<Long> getTodayAdd() throws ParseException {
		Date date = new Date();
		date = TimeUtils.getSqlDate(date);
		LambdaQueryWrapper<AbsentTable> lambdaQueryWrapper =new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(AbsentTable::getAbsDate,date);
		Long num = absentTableService.count(lambdaQueryWrapper);
		return  R.success(num);
	}
}

